package edu.ntu.im.p2ptrade.msg;

import java.util.List;

import edu.ntu.im.p2ptrade.P2PTrade;
import edu.ntu.im.p2ptrade.entity.Item;

import net.jxta.endpoint.Message;
import net.jxta.endpoint.StringMessageElement;

public class SearchResult implements PMessage {
	String queryTerm;
	List<Item> items;
	public SearchResult(List<Item> items, String queryTerm){
		this.items = items;
		this.queryTerm = queryTerm;
	}

	public Message getMessageToTransfer() {
		Message msg = new Message();
		MyItems.Items2Message(msg, items);
		msg
		.addMessageElement(P2PTrade.getInstance().getAppName(),
				new StringMessageElement(SearchItemQuery.QUERY,this.queryTerm, null));
		msg
		.addMessageElement(P2PTrade.getInstance().getAppName(),
				new StringMessageElement(PEER,P2PTrade.getInstance().getPeerName(), null));
		msg
		.addMessageElement(P2PTrade.getInstance().getAppName(),
				new StringMessageElement(TYPE,SearchResult.class
						.getSimpleName(), null));
		
		return msg;
	}

	public String toLogMessage() {
		StringBuffer sb = new StringBuffer();
		sb.append("Respond to query:["+this.queryTerm+"]");
		for (Item item:items){
			sb.append(items.toString());
		}
		return sb.toString();
	}

}
